package com.dx_society.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dx_society.domain.UserDetail;
import com.dx_society.dto.Result;
import com.dx_society.dto.UserDetailDTO;
import com.dx_society.service.UserDetailService;
import com.dx_society.mapper.UserDetailMapper;
import com.dx_society.utils.UserHolder;
import org.springframework.stereotype.Service;

/**
* @author 韶光善良君
* @description 针对表【dx_user_detail(用户详情表)】的数据库操作Service实现
* @createDate 2024-07-01 10:15:38
*/
@Service
public class UserDetailServiceImpl extends ServiceImpl<UserDetailMapper, UserDetail>
    implements UserDetailService{

    @Override
    public Result queryUserDetail() {
        Long userId;
        try {
            userId = UserHolder.getUser().getUserid();
        } catch (Exception e) {
            return Result.fail("用户未登录");
        }

        UserDetail userDetail = getById(userId);

        if (userDetail == null) {
            return Result.fail("该用户还没有设置用户详情");
        }else {
            return Result.ok(userDetail);
        }

    }

    @Override
    public Result setUserDetail(UserDetailDTO userDetailDTO) {
        Long userId;
        try {
            userId = UserHolder.getUser().getUserid();
        } catch (Exception e) {
            return Result.fail("用户未登录");
        }

        UserDetail userDetail = new UserDetail();
        userDetail.setUserid(userId);
        userDetail.setAge(userDetailDTO.getAge());
        userDetail.setGender(userDetailDTO.getGender());
        userDetail.setMajor(userDetailDTO.getMajor());
        userDetail.setPhone(userDetailDTO.getPhone());

        if (saveOrUpdate(userDetail)) {
            return Result.ok("设置已更改");
        }else {
            return Result.fail("设置失败");
        }
    }
}




